﻿using UnityEngine;

public class BubbleSpawner : MonoBehaviour
{
    public GameObject bubblePrefab;
    public Transform bubbleParent;
    public static BubbleSpawner Instance;
    public long bubbleId = 0;

    private Sprite[] _bubbleSprites;
    public Color[] _bubbleColors = new Color[10];
    public int bubbleCount;

    private void Awake()
    {
        Instance = this;
        _bubbleSprites = new Sprite[10];
        Warm();
    }

    private void Warm()
    {
        //TODO 资源加载
        /*ResourcesComponent resourcesComponent = Game.Scene.GetComponent<ResourcesComponent>();
        string bundleName = "gameresources.unity3d";
        resourcesComponent.LoadBundle(bundleName);
        for (int i = 0; i < 10; i++)
        {
            string spriteName = $"bubble_{i}";
            _bubbleSprites[i] = resourcesComponent.GetSprite(bundleName, spriteName);
        }*/
    }

    private void Start()
    {
    }

    public Sprite GetSpriteByNumber(int number)
    {
        int spriteIndex = number % this._bubbleSprites.Length;
        return this._bubbleSprites[spriteIndex];
    }

    public Color GetColor(int number)
    {
        return this._bubbleColors[number % this._bubbleColors.Length];
    }

    public void GameStart()
    {
        this.bubbleId = 0;
    }

    public Bubble GetBubble(long id)
    {
        this.bubbleId = id;
        GameObject go = Instantiate(this.bubblePrefab);
        go.transform.SetParent(this.bubbleParent);
        Bubble bubble = go.GetComponent<Bubble>();
        bubble.id = this.bubbleId;
        return bubble;
    }

    public Bubble GetBubble()
    {
        bubbleId++;
        GameObject go = Instantiate(this.bubblePrefab);
        go.transform.SetParent(this.bubbleParent);
        Bubble bubble = go.GetComponent<Bubble>();
        bubble.id = this.bubbleId;
        return bubble;
    }

    public int GetBubbleCount()
    {
        bubbleCount = this.bubbleParent.childCount - 12;
        return bubbleCount;
    }
}